package LeetCode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class No17 {
    public static void main(String[] args) {
        letterCombinations("23");
    }

    public static List<String> letterCombinations(String digits) {
        List<String> result = new ArrayList<>();
        Map<String,String> map = new HashMap<>();
        map.put("2","abc");
        map.put("3","def");
        map.put("4","ghi");
        map.put("5","jkl");
        map.put("6","mno");
        map.put("7","pqrs");
        map.put("8","tuv");
        map.put("9","wxyz");
        def(result,0,digits,new StringBuffer(),map);
        System.out.println(result);
        return result;
    }

    public static void def(List<String> result, int index,String digits,StringBuffer s,Map<String,String> map){
        if(index==digits.length()){
            result.add(s.toString());
        }else {
            String letter = map.get(Character.toString(digits.charAt(index)));
            for (int i = 0; i < letter.length(); i++) {
                s.append(letter.charAt(i));
                def(result,index+1,digits,s,map);
                s.deleteCharAt(s.length()-1);
            }
        }
    }
}
